#!/usr/bin/python
# -*- coding:utf-8 -*-
# @author  : micah
# @time    : 2023/12/1 22:48
# @function: the script is used to do something.
# @version :

import json

"""字典与json互转"""
dict_data_1 = {'name': '安娜', 'age': 18}
# ensure_ascii=False 显示中文， indent=4 缩进为4
json_data = json.dumps(dict_data_1, ensure_ascii=False, indent=4)
print('字典转json：', json_data)

print('json转字典:', json.loads(json_data))

"""
json数据写入与json数据读取并转为dict对象
"""
data_dict = {"name": "双双", 'age': 22}

with open('测试文件.txt', 'w') as f:
    json.dump(data_dict, f, ensure_ascii=False, indent=4)


with open('测试文件.txt', 'r') as f:
    my_dict = json.load(f)
    print(my_dict, type(my_dict))
